package interfaceObjects;

public class Scanner {
	final String NO_ERROR = "NO_ERROR";
	final String ERROR = "ERROR";

	final String PLUS = "PLUS";
	final String MINUS = "MINUS";
	final String MAL = "MAL";
	final String DURCH = "DURCH";
	final String POTENZ = "POTENZ";
	final String KLAMMER_AUF = "KLAMMER_AUF";
	final String KLAMMER_ZU = "KLAMMER_ZU";
	final String ENDE = "ENDE";

	final String CONST = "KONSTANTE";

	public Scanner() {
	}

	private String token;

	public void scanString(String input) {
		String lexem = "";
		int index = 0;
		int error = 0;
		while (input.charAt(index) != ';') {
			switch (input.charAt(index)) {
			case '+':
				lexem = String.valueOf(input.charAt(index));
				token = PLUS;
				System.out.println("token: " + token + " lexem: " + lexem + " Error-Code: " + error);
				index++;
				break;
			case '-':
				lexem = String.valueOf(input.charAt(index));
				token = MINUS;
				System.out.println("token: " + token + " lexem: " + lexem + " Error-Code: " + error);
				index++;
				break;
			case '/':
				lexem = String.valueOf(input.charAt(index));
				token = DURCH;
				System.out.println("token: " + token + " lexem: " + lexem + " Error-Code: " + error);
				index++;
				break;
			case '*':
				lexem = String.valueOf(input.charAt(index));
				token = MAL;
				System.out.println("token: " + token + " lexem: " + lexem + " Error-Code: " + error);
				index++;
				break;
			case '^':
				lexem = String.valueOf(input.charAt(index));
				token = POTENZ;
				System.out.println("token: " + token + " lexem: " + lexem + " Error-Code: " + error);
				index++;
				break;
			case '(':
				lexem = String.valueOf(input.charAt(index));
				token = KLAMMER_AUF;
				System.out.println("token: " + token + " lexem: " + lexem + " Error-Code: " + error);
				index++;
				break;
			case ')':
				lexem = String.valueOf(input.charAt(index));
				token = KLAMMER_ZU;
				System.out.println("token: " + token + " lexem: " + lexem + " Error-Code: " + error);
				index++;
				break;
			default:
				if (Character.isDigit(input.charAt(index))) {
					boolean valid = true;
					boolean isKommaGesetzt = false;
					token = CONST;
					lexem = String.valueOf(input.charAt(index));
					while (valid) {
						if(Character.isDigit(input.charAt(index + 1))){
							lexem += String.valueOf(input.charAt(index + 1));
							valid = true;
						} else if (input.charAt(index+1) == '.' && !isKommaGesetzt) {
							lexem += String.valueOf(input.charAt(index+1));
							valid = true;
							isKommaGesetzt = true;
						} else{ 
							valid = false;
						}
						index++;
					}
					System.out.println("token: " + token + " lexem: " + lexem
							+ " Error-Code: " + error);
				} else {
					error = 1;
					lexem = String.valueOf(input.charAt(index));
					System.out.println( "          lexem: " + lexem
							+ " Error-Code: " + error);
					error = 0;
					index++;
				}
				break;
			}

			// if (input.charAt(index) == '+') {
			// lexem = String.valueOf(input.charAt(index));
			// token = PLUS;
			// System.out.println("token: " + token + " lexem: +");
			// index++;
			// } else if (Character.isDigit(input.charAt(index))) {
			// token = CONST;
			// lexem = String.valueOf(input.charAt(index));
			// System.out.println("token: " + lexem + " lexem: " + INT);
			// while (Character.isDigit(input.charAt(index + 1))) {
			// lexem += String.valueOf(input.charAt(index + 1));
			// index++;
			// }
			// index++;
			// }
		} 
		lexem = String.valueOf(input.charAt(index));
		System.out.println("token: " + ENDE + " lexem: " + lexem
							+ " Error-Code: " + error);
	}
}